﻿using Shop.Model.DB;
using Shop.RemoteModel.PageModule.Model;
using Shop.RemoteModel.PageModule.ModuleConfig;
using Shop.Repository;
using WeDonekRpc.Helper;

namespace Shop.Collect.lmpl
{
    internal class PageModuleCollect : IPageModuleCollect
    {
        private readonly IPageModuleRepository _Repository;

        public PageModuleCollect ( IPageModuleRepository repository )
        {
            this._Repository = repository;
        }

        public DBPageModule Get ( long id )
        {
            return this._Repository.Get(id);
        }
        public long Add ( PageModuleAdd data )
        {
            if ( this._Repository.IsExist(c => c.TemplateId == data.TemplateId && c.ModuleTitle == data.ModuleTitle) )
            {
                throw new ErrorException("shop.page.module.title.repeat");
            }
            return this._Repository.Add(data);
        }

        public void SetSort ( ModuleSortSet[] sorts )
        {
            this._Repository.SetSort(sorts);
        }
        public bool Set ( DBPageModule source, PageModuleSet set )
        {
            if ( set.ModuleTitle != source.ModuleTitle && this._Repository.IsExist(c => c.TemplateId == source.TemplateId && c.ModuleTitle == set.ModuleTitle) )
            {
                throw new ErrorException("shop.page.module.title.repeat");
            }
            return this._Repository.Update(source, set);
        }
        public Result[] Gets<Result> ( long templateId ) where Result : class, new()
        {
            return this._Repository.Gets<Result>(a => a.TemplateId == templateId, "Sort");
        }

        public void Delete ( DBPageModule source )
        {
            this._Repository.Delete(source.Id);
        }
    }
}
